<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .menuBar {
            width: 200px;
            line-height: 36px;
            font-size: 14px;
            position: fixed;
            left: 0;
            top: 0;
            border: 1px solid #999;
            display: none;
        }

        .menuBar .menu {
            padding: 5px;
            text-indent: 10px;
        }

        .menuBar .menu li+li {
            border-top: 1px solid #999;
        }
    </style>
</head>

<body>
    <div class="menuBar">
        <ul class="menu">
            <li class="copy">复制</li>
            <li class="paste">粘贴</li>
            <li class="cut">剪切</li>
            <li class="refresh">刷新</li>
        </ul>
    </div>
</body>
<script>
    var menuBar = document.querySelector(".menuBar");
    document.oncontextmenu = function (e) {
        var e = e || window.event;
        // 先阻止默认菜单
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }

        // 再将自己的显示出来
        var x = e.clientX;
        var y = e.clientY;

        menuBar.style.left = x + "px";
        menuBar.style.top = y + "px";
        menuBar.style.display = "block";
    }

    document.onmousedown = function () {
        menuBar.style.display = "none";
    }

    menuBar.onmousedown = function (e) {
        var e = e || window.event;

        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    }

    menuBar.onclick = function (e) {
        var e = e || window.event;
        var target = e.target || e.srcElement;

        if (target.className == "copy") {
            console.log("点击了复制");
        } else if (target.className == "paste") {
            console.log("点击了粘贴");
        } else if (target.className == "cut") {
            console.log("点击了剪切");
        } else if (target.className == "refresh") {
            console.log("点击了刷新");
            location.reload();
        }
        menuBar.style.display = "none";
    }



</script>

</html>